package class61To70;

import java.io.*;
import java.util.Arrays;


// https://www.luogu.com.cn/problem/P4779
public class DijkstraAndHeap {

    public static int MAX_N = 100001;

    public static int MAX_M = 200001;

    public static int[] head = new int[MAX_N];

    public static int[] next = new int[MAX_M];

    public static int[] to = new int[MAX_M];
    public static int[] weight = new int[MAX_M];

    public static int cnt;

    public static int[] heap = new int[MAX_N];

    public static int[] where = new int[MAX_N];

    public static int[] distance = new int[MAX_N];

    public static int heapSize;

    public static void build(int n) {
        heapSize = 0;
        cnt = 1;
        Arrays.fill(head, 0, n + 1, 0);
        Arrays.fill(where, 0, n + 1, -1);
        Arrays.fill(distance, 0, n + 1, Integer.MAX_VALUE);
    }

    public static void addEdge(int u, int v, int w) {
        next[cnt] = head[u];
        to[cnt] = v;
        weight[cnt] = w;
        head[u] = cnt++;
    }

    public static void swap(int i, int j) {
        int tmp = heap[i];
        heap[i] = heap[j];
        heap[j] = tmp;
        where[heap[i]] = i;
        where[heap[j]] = j;
    }

    public static void heapInsert(int i) {
        while (distance[heap[i]] < distance[heap[(i - 1) / 2]]) {
            swap(i, (i - 1) / 2);
            i = (i - 1) / 2;
        }
    }

    public static int pop() {
        int ans = heap[0];
        swap(0, --heapSize);
        where[ans] = -2;
        heapIfy(0);
        return ans;
    }

    public static void heapIfy(int i) {
        int l = 2 * i + 1;
        while (l < heapSize) {
            int best = l + 1 < heapSize && distance[heap[l + 1]] < distance[heap[l]] ? l + 1 : l;
            best = distance[heap[best]] < distance[heap[i]] ? best : i;
            if (best == i) {
                break;
            }
            swap(i, best);
            i = best;
            l = i * 2 + 1;
        }
    }

    public static boolean isEmpty() {
        return heapSize == 0;
    }

    public static void addOrUpdateOrIgnore(int v, int w) {
        if (where[v] == -1) {
            heap[heapSize] = v;
            where[v] = heapSize++;
            distance[v] = w;
            heapInsert(where[v]);
        } else if (where[v] >= 0) {
            distance[v] = Math.min(distance[v], w);
            heapInsert(where[v]);
        }
    }

    public static int n, m, s, u, v, w;


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            n = (int) in.nval;
            in.nextToken();
            m = (int) in.nval;
            in.nextToken();
            s = (int) in.nval;
            build(n);
            for (int i = 0; i < m; i++) {
                in.nextToken();
                u = (int) in.nval;
                in.nextToken();
                v = (int) in.nval;
                in.nextToken();
                w = (int) in.nval;
                addEdge(u, v, w);
            }
            slove(s);
            for (int i = 1; i <= n; i++) {
                out.print(distance[i] + " ");
            }
        }
        out.flush();
        out.close();
        br.close();
    }

    private static void slove(int s) {
        addOrUpdateOrIgnore(s, 0);
        while (!isEmpty()) {
            int cur = pop();
            for (int ei = head[cur]; ei > 0; ei = next[ei]) {
                addOrUpdateOrIgnore(to[ei], distance[cur] + weight[ei]);
            }
        }
    }
}
